package MT::App::TypeKey;
use strict;
use warnings;

use base 'MT::App';

use MT::TypeKey;
use MT::Util qw( encode_url is_valid_email );

use URI;

sub init {
    my $app = shift;
    $app->SUPER::init(@_) or return;
    $app->add_methods(signon  => \&signon,
                      signoff => \&signoff,);
    $app->{default_mode} = 'signon';
    $app;
}

sub init_request
{    # init_request apparently does check for errors so we are forced to die
    my $app = shift;
    $app->SUPER::init_request(@_) or return;
    die $app->translate(  "Sign in requires a secure signature; "
                        . "logout requires the logout=1 parameter")
      unless $app->param('sig') || $app->param('logout');
    for (qw(tkapp statemgr)) {    # dumb
        delete $app->{$_};
    }
    my $appkey = $app->param('app');
    $app->{tkapp} = MT::TypeKey->new($appkey) or die MT::TypeKey->errstr;
    MT->request->stash("MT::TypeKey::${appkey}", $app->{tkapp});
    my $mode = $app->param('logout') ? 'signoff' : 'signon';
    $app->param('__mode', $mode)
      ; # bummer that you have to store the mode in the param data unlike CGI::Application.
    $app;
}

# login, signon, signin -- argh!

sub signon {
    my $app   = shift;
    my $q     = $app->{query};
    my $tkapp = $app->{tkapp};
    my $smgr  = $tkapp->state_mgr;
    unless ($tkapp->validate($q)) {
        return $app->error('The validation failed: ' . $tkapp->errstr);
    }
    my $email = $q->param('email');
    return $app->error("This weblog requires users to pass an email "
          . "address. If you'd like to do so you may log in again, and give "
          . "the authentication service permission to pass your email address.")
      if ($tkapp->need_email && !is_valid_email($email));
    my %s = (    # Signature was valid, so create a session, etc.
              id    => $q->param('sig'),
              name  => $q->param('name'),
              email => $q->param('email')
    );           # start and kind handled by state mgr.
    my $session = $smgr->create_session(\%s)
      || return $app->error($smgr->errstr);
    if ($ENV{MOD_PERL}) {
        my $p = $q->parms;
        map { $p->unset($_) } qw( app sig name email nick ts token __mode );
    } else {
        map { $q->delete($_) } qw( app sig name email nick ts token __mode);
    }
    $q->param('signin', 1);    # comment (default) tk driver only ???
    my $url = $tkapp->post_signon_link or return $app->error($tkapp->errstr);
    $app->redirect($url, UseMeta => 1);
}

sub signoff {
    my $app     = shift;
    my $q       = $app->{query};
    my $tkapp   = $app->{tkapp};
    my $smgr    = $tkapp->state_mgr;
    my $session = $smgr->get_session;
    $smgr->expire_session($session) if $session;
    if ($ENV{MOD_PERL}) {
        my $p = $q->parms;    # ????
        map { $p->unset($_) } qw( app logout __mode );
    } else {
        map { $q->delete($_) } qw( app logout __mode );
    }
    my $url = $tkapp->post_signoff_link or return $app->error($tkapp->errstr);
    $app->redirect($url, UseMeta => 1);
}

1;

__END__

=head1 NAME

MT::App::TypeKey - a gateway for all incoming TypeKey
information for the system

=head1 VERSION

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=head1 AUTHOR & COPYRIGHT

MT::App::TypeKey is Copyright 2005-2007, Timothy Appnel,
Appnel Internet Solutions, LLC. http://appnel.com/

This program is free software; you can use, modify, and
redistribute it under the same terms as Perl 5.8.x itself.

See http://www.perl.com/perl/misc/Artistic.html

=cut
